﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using Common;
using WcfService.DataContracts;

namespace Visualizers.ServerConnecter
{
    internal class wpOrtConnector : IDisposable
    {
        private readonly Iwp7ortClient _wpClient;

        public wpOrtConnector(IpeRemoteLookup ipeRemoteLookup)
        {
            const int bufferSize = 2147483647;
            BasicHttpBinding binding = new BasicHttpBinding
                                           {
                                               MaxReceivedMessageSize = bufferSize,
                                               MaxBufferSize = bufferSize,
                                           };

            binding.ReaderQuotas.MaxArrayLength = bufferSize;

            

            string uri = ipeRemoteLookup == IpeRemoteLookup.LocalHost
                             ? "http://eee/wp7ort.svc"
                             : "http://wp7ort.apporist.com/wp7ort.svc";

            var endpoint = new EndpointAddress(uri);

            _wpClient = new Iwp7ortClient(binding, endpoint);
        }

        public List<TagInfo> GetImagesMetaData()
        {
            GetImagesResponse result = _wpClient.GetImages(new GetImagesRequest());
            return result.Images.ToList();
        }

        public ImageData DownloadImageData(TagInfo tagInfo)
        {
            return _wpClient.GetImage(tagInfo);
        }

        public void Dispose()
        {
            _wpClient.Close();
        }
    }
}
